import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { User } from '@/types/user'

/**
 * 参数1：仓库的名字
 * 参数2：箭头函数，写仓库逻辑，现在写组合式
 * 参数3：持久化（默认存储到localstorage）
 */
export const useUserStore = defineStore(
  'cp-user',
  () => {
    const user = ref({} as User)
    const setUser = (val: User) => {
      user.value = val
    }

    const clear = () => {
      user.value = {} as User
    }

    return {
      user,
      setUser,
      clear
    }
  },
  {
    persist: true // 持久化
  }
)
